package com.ruoyi.assets.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ruoyi.assets.domain.AssetCategories;
import com.ruoyi.assets.domain.AssetFinancial;
import com.ruoyi.assets.domain.AssetMethods;
import com.ruoyi.assets.dto.BasicDTO;
import com.ruoyi.assets.dto.FinancialDTO;
import com.ruoyi.assets.dto.MethodsDTO;
import com.ruoyi.assets.mapper.AssetCategoriesMapper;
import com.ruoyi.assets.mapper.AssetFinancialMapper;
import com.ruoyi.assets.mapper.AssetMethodsMapper;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.assets.mapper.AssetBasicMapper;
import com.ruoyi.assets.domain.AssetBasic;
import com.ruoyi.assets.service.IAssetBasicService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 固定资产Service业务层处理
 * 
 * @author 孙帆科
 * @date 2025-03-04
 */
@Service
public class AssetBasicServiceImpl implements IAssetBasicService
{
    @Autowired
    private AssetBasicMapper assetBasicMapper;

    @Autowired
    private AssetCategoriesMapper assetCategoriesMapper;

    @Autowired
    private AssetMethodsMapper assetMethodsMapper;

    @Autowired
    private AssetFinancialMapper assetFinancialMapper;

    /**
     * 查询固定资产
     * 
     * @param basicId 固定资产主键
     * @return 固定资产
     */
    @Override
    public AssetBasic selectAssetBasicByBasicId(Long basicId)
    {
        return assetBasicMapper.selectAssetBasicByBasicId(basicId);
    }

    /**
     * 查询固定资产列表
     * 
     * @param assetBasic 固定资产
     * @return 固定资产
     */
    @Override
    public List<AssetBasic> selectAssetBasicList(AssetBasic assetBasic)
    {
        return assetBasicMapper.selectAssetBasicList(assetBasic);
    }

    /**
     * 新增固定资产
     * @param assetBasic 固定资产
     * @return 结果
     */
    @Override
    public int insertAssetBasic(AssetBasic assetBasic)
    {
        return assetBasicMapper.insertAssetBasic(assetBasic);
    }

    /**
     * 修改固定资产
     * 
     * @param assetBasic 固定资产
     * @return 结果
     */
    @Override
    public int updateAssetBasic(AssetBasic assetBasic)
    {
        return assetBasicMapper.updateAssetBasic(assetBasic);
    }

    /**
     * 批量删除固定资产
     * 
     * @param basicIds 需要删除的固定资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetBasicByBasicIds(Long[] basicIds)
    {
        return assetBasicMapper.deleteAssetBasicByBasicIds(basicIds);
    }

    /**
     * 删除固定资产信息
     * 
     * @param basicId 固定资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetBasicByBasicId(Long basicId)
    {
        return assetBasicMapper.deleteAssetBasicByBasicId(basicId);
    }

    /**
     * 批量新增固定资产
     * @param basicDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int additionAssets(BasicDTO basicDTO) {
        if (basicDTO == null) {
            throw new ServiceException("请求参数不能为空");
        }

        // 1. 处理基本信息
        AssetBasic assetBasic = new AssetBasic();
        BeanUtils.copyProperties(basicDTO, assetBasic);
        assetBasic.setEntryPeriod(new Date());
        assetBasic.setCreateTime(new Date());
        assetBasic.setAdditionType(basicDTO.getAssetType());
        assetBasic.setAdditionType(0L); // 设置凭证状态为未生成

        // 2. 处理财务信息
        FinancialDTO financialDTO = basicDTO.getFinancialDTOList();
        if (financialDTO != null) {
            AssetFinancial assetFinancial = new AssetFinancial();
            BeanUtils.copyProperties(financialDTO, assetFinancial);
            assetBasic.setFinancialId(financialDTO.getFinancialId());
//            assetFinancial.setAssetId(assetBasic.getBasicId());
            assetFinancial.setCreateTime(new Date());

            // 计算预计净残值
//            if (financialDTO.getOriginalValue() != null && financialDTO.getSalvageRate() != null) {
//                BigDecimal estimatedSalvageValue = financialDTO.getOriginalValue()
//                        .compareTo(financialDTO.getSalvageRate())
//                        .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
//                assetFinancial.setEstimatedSalvageValue(estimatedSalvageValue);
//            }

            int financialRows = assetFinancialMapper.insertAssetFinancial(assetFinancial);
            if (financialRows <= 0) {
                throw new ServiceException("新增资产财务信息失败");
            }
            assetBasic.setFinancialId(assetFinancial.getFinancialId());
        }

        // 3. 处理折旧方法信息
        MethodsDTO methodsDTO = basicDTO.getMethodsDTOList();
        if (methodsDTO != null) {
            AssetMethods assetMethods = new AssetMethods();
            BeanUtils.copyProperties(methodsDTO, assetMethods);
            assetBasic.setMethodId(methodsDTO.getMethodsId());
//            assetMethods.setAssetId(assetBasic.getBasicId());
            assetMethods.setCreateTime(new Date());

            int methodRows = assetMethodsMapper.insertAssetMethods(assetMethods);
            if (methodRows <= 0) {
                throw new ServiceException("新增资产折旧方法信息失败");
            }
            assetBasic.setMethodId(assetMethods.getMethodsId());
        }

        // 4. 更新资产基本信息的关联ID
        assetBasicMapper.updateAssetBasic(assetBasic);


        int rows = assetBasicMapper.insertAssetBasic(assetBasic);
        if (rows <= 0) {
            throw new ServiceException("新增资产基本信息失败");
        }


        return rows;
    }

    @Override
    public BasicDTO basicDetailsList(Long basicId) {
        return assetBasicMapper.basicDetailsList(basicId);
    }
}
